package cc.catface.service.admin;

import cc.catface.common.output.ResultBody;
import cc.catface.dao.admin.dto.ChannelListDetailOutputDTO;
import cc.catface.dao.admin.mapper.AdChannelLabelMapperServiceImpl;
import cc.catface.dao.admin.mapper.AdChannelMapperServiceImpl;
import cc.catface.dao.admin.mapper.AdLabelMapperServiceImpl;
import cc.catface.dao.admin.pojo.AdChannel;
import cc.catface.dao.admin.pojo.AdChannelAddInputDTO;
import cc.catface.dao.admin.pojo.AdChannelLabel;
import cc.catface.dao.admin.pojo.AdLabel;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AdChannelServiceImpl implements AdChannelService {
    @Autowired
    private AdChannelMapperServiceImpl channelMapperService;
    @Autowired
    private AdChannelLabelMapperServiceImpl channelLabelMapperService;
    @Autowired
    private AdLabelServiceImpl labelService;
    @Autowired
    private AdLabelMapperServiceImpl labelMapperService;

    @Override
    public ResultBody get(String name) {
        QueryWrapper<AdChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        AdChannel channel = channelMapperService.getOne(wrapper);
        if (channel == null) return ResultBody.error("频道具体信息查询失败");
        return ResultBody.success(channel);
    }

    @Override
    public ResultBody add(AdChannelAddInputDTO input) {
        //检查标签表有无该labelId
        Integer labelId = input.getLabelId();
        long labelIdCount = labelMapperService.count(new QueryWrapper<AdLabel>().eq("id", labelId));
        if (labelIdCount < 1) return ResultBody.error("无当前标签ID");

        ResultBody resultBody = get(input.getChannel().getName());
        if (resultBody.getData() != null) return ResultBody.error("当前频道已存在");
        boolean add = channelMapperService.save(input.getChannel());
        if (add) {
            AdChannelLabel channelLabel = new AdChannelLabel();
            channelLabel.setLabelId(labelId);
            channelLabel.setChannelId(input.getChannel().getId());
            boolean save = channelLabelMapperService.save(channelLabel);
            if (save) return ResultBody.success();
            return ResultBody.error("添加频道标签关联表失败");
        }
        return ResultBody.error("频道添加失败");
    }

    @Override
    public ResultBody update(AdChannel channel) {
        boolean update = channelMapperService.updateById(channel);
        if (update) return ResultBody.success();
        return ResultBody.error("频道信息更新失败");
    }

    @Override
    public ResultBody list() {
        return ResultBody.success(channelMapperService.list());
    }

    @Override
    public ResultBody listDetail() {
        List<AdChannelLabel> channelLabels = channelLabelMapperService.list();
        log.info("(listDetail)所有channelLabels:{}", JSON.toJSONString(channelLabels));
        Set<Integer> labelIds = channelLabels.stream().map(AdChannelLabel::getLabelId).collect(Collectors.toSet());
        log.info("(listDetail)所有labelIds:{}", Arrays.toString(labelIds.toArray()));

        List<ChannelListDetailOutputDTO> output = new ArrayList<>();
        for (Integer labelId : labelIds) {
            ChannelListDetailOutputDTO dto = new ChannelListDetailOutputDTO();
            String labelName = labelMapperService.getOne(new QueryWrapper<AdLabel>().eq("id", labelId)).getName();
            log.info("(listDetail)根据labelId:{}, 获取的labelName:{}", labelId, labelName);
            dto.setLabelName(labelName);
            dto.setLabelId(labelMapperService.getOne(new QueryWrapper<AdLabel>().eq("id", labelId)).getId());

            List<Integer> channelIds = channelLabels.stream().filter(adChannelLabel -> adChannelLabel.getLabelId().equals(labelId)).map(AdChannelLabel::getChannelId).collect(Collectors.toList());
            log.info("(listDetail), labelId:{}, 对应的channelIds:{}", labelId, Arrays.toString(channelIds.toArray()));


            List<ChannelListDetailOutputDTO.Channel> channels = new ArrayList<>();
            List<AdChannel> adChannels = channelMapperService.list(new QueryWrapper<AdChannel>().in("id", channelIds));
            if (adChannels != null) {
                for (AdChannel channel : adChannels) {
                    ChannelListDetailOutputDTO.Channel c = new ChannelListDetailOutputDTO.Channel();
                    c.setChannelName(channel.getName());
                    c.setChannelId(channel.getId());
                    channels.add(c);
                }
            }
            dto.setChannels(channels);
            //List<String> channelNames = channelMapperService.list(new QueryWrapper<AdChannel>().in("id", channelIds)).stream().map(AdChannel::getName).collect(Collectors.toList());
            //dto.setChannelName(channelNames);


            output.add(dto);
        }
        return ResultBody.success(output);
    }
}
